<template>
	<view>
		<sa-tabs :tabList="topList" @onClickTab="clickTab"></sa-tabs>

		<view class="scroll-view-container" :style="{height:wh + 'px'}">
			<!-- 左侧滑动区 -->
			<scroll-view class="left-scroll-view" scroll-y="true" :style="{height:wh + 'px'}">
				<view :class="['left-scroll-view-item', 0 === active ? 'active':'']" @click="activeChange(0)">全部
				</view>
				<block v-for="(item,i) in goodsTypes" :key="item.id">
					<view
						:class="['left-scroll-view-item', item.id === active ? 'active':'', item.type == 1 ? '': 'yingxiao']"
						@click="activeChange(item)">
						{{item.name}}
					</view>
				</block>
			</scroll-view>
			<!-- 右侧滑动区 -->
			<scroll-view scroll-y="true" :style="{height:wh + 'px',backgroundColor:'#fff'}" :scroll-top="scorllTop">
				<template v-if="goodsList.length">
					<uni-list>
						<uni-list-item v-for="(item,i) in goodsList" :key="item.id" >
							<template v-slot:header>
								<view class="goods-header">
									<image :src="goodsImage(item)" class="goods-image" mode="scaleToFill" @click="selectGoods(item)"></image>
								</view>
							</template>
							<template v-slot:body>
								<view class="goods-intro">
									<view class="goods-top">
										<view class="goods-name">{{item.name}}</view>
										<view class="goods-jieshao">{{item.description}}</view>
									</view>
									<view class="goods-bottom">
										<view class="goods-price">{{priceTransform(item.price)}}/{{item.specification}}
										</view>
									</view>
								</view>
							</template>
						</uni-list-item>
					</uni-list>
				</template>
				<template v-else>
					<view class="no-info">~~暂无信息~~</view>
				</template>

			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		getGoodsList,
	} from '@/api/shops/goodsManage/goodsManage'
	import {
		getGoodsType,
	} from '@/api/shops/categoryManage/categoryManage.js'
	import {
		transform
	} from '@/minix/mixin.js'
	import {
		addGoods2Market
	} from '@/api/shops/marketAddGoods/marketAddGoods.js'
	export default {
		mixins: [transform],
		data() {
			return {
				shopId: '',
				type: 1,
				eventChannel: undefined,
				wh: 0,
				active: 0,
				shopId: '',
				saleState: 1, // 销售状态 默认为销售中
				goodsTypes: [],
				goodsList: [],
				selGoods: [],

				checkedGoods: new Map(),
				selectCateId: '',
				cateType: 1,
				selSingleGoods: [],
				opeaMode: 0, // 操作模式，0：单个操作，1：批量操作
				hasChange:false,


				topList: [{
					text: "销售中"
				}, {
					text: "已下架"
				}],

				pattern: {
					color: '#7A7E83',
					backgroundColor: '#fff',
					selectedColor: '#007AFF',
					buttonColor: '#007AFF',
					iconColor: '#fff'
				},
				directionStr: '垂直',
				horizontal: 'right',
				vertical: 'bottom',
				direction: 'horizontal',
			}
		},
		onLoad(options) {
			this.eventChannel = this.getOpenerEventChannel();
			this.shopId = options.shopId
			this.type = options.type
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - sysInfo.safeAreaInsets.bottom - 40
			this.init()
		},
		computed: {
			goodsImage: () => {
				return (item) => {
					return item.multimediaUrl.split(",")[0] || ''
				}
			},
			checkGoods: function(item) {
				return (item) => {
					return this.selGoods.indexOf(item.id) > -1
				}
			},
			showOpra() {
				return this.selGoods.length > 0;
			},
			itemList() {
				if (this.opeaMode == 0) {
					return ['移动分组', `${this.saleState?'下架':'上架'}`, '删除', '编辑'];
				} else {
					return ['移动分组', `${this.saleState?'下架':'上架'}`, '删除'];
				}
		
			}
		},
		methods: {
			async init() {

				const res = await getGoodsType(this.shopId)
				this.goodsTypes = res

				this.getGoodsListInfo();

			},
			getGoodsListInfo() {
				let form = {
					shopId: this.shopId,
					goodsType: this.selectCateId,
					state: this.saleState,
					type: this.cateType
				}
				getGoodsList(form).then((res) => {
					this.goodsList = res

				})
			},
			clickTab(e) {
				// 销售中
				if (e.index == 0) {
					this.saleState = 1
				} else {
					this.saleState = 0
				}
				this.selGoods = []
				this.getGoodsListInfo();
			},
			activeChange(index) {
				// 全部商品
				if (index === 0) {
					this.cateType = 1;
					this.active = 0;
					this.selectCateId = ''
				} else {
					this.cateType = index.type;
					this.active = index.id
					this.selectCateId = index.id
				}
				this.getGoodsListInfo()
			},
			selectGoods(goods) {
				let _this = this
				uni.showModal({
					title:'选择该商品参加活动',
					success: (res) => {
						if(res.confirm) {
							addGoods2Market({
								goodsId:goods.id,
								num:1,
								times:1,
								state:0
							}).then((r) => {
								_this.hasChange = true
								uni.showModal({
									title:'添加成功,是否返回?',
									success: (a) => {
										if(a.confirm) {
											uni.navigateBack()
										}
									}
								})
								_this.eventChannel.emit('selectGoods',{data:true})
							}).catch((r) => {
								uni.showToast({
									title:r,
									icon:'none'
								})
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
		color: transparent;
	}

	.scroll-view-container {
		display: flex;
		overflow: hidden;

		.left-scroll-view {
			width: 120px;

			.left-scroll-view-item {
				background-color: #f7f7f7;
				line-height: 40px;
				text-align: center;
				font-size: 14px;

				&.active {
					background-color: #ffffff;
					position: relative;

					&::before {
						content: ' ';
						display: block;
						width: 2px;
						height: 100%;
						background-color: $base-color;
						position: absolute;
						top: 50%;
						right: 0;
						transform: translateY(-50%);
					}
				}

				&.yingxiao {
					color: red;
				}
			}
		}
	}

	.goods-header {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.goods-image {
		width: 150rpx;
		height: 150rpx;
		border-radius: 5px;
	}

	.goods-intro {
		margin-left: 5px;
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;

		.goods-top {
			display: flex;
			flex-direction: column;
			flex: 1;

			.goods-name {
				font-size: 14px;
				font-weight: bold;
			}

			.goods-jieshao {
				font-size: 12px;
				color: #a5a099;
				-webkit-line-clamp: 3;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				word-break: break-all;
				overflow: hidden;
			}
		}

		.goods-price {}
	}

	.goods-opre {
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
		align-items: flex-end;
	}

	.batch-opa {
		position: fixed;
		bottom: 10px;
		right: 10px;
	}

	.batch-btn {
		background-color: $base-color;
		color: #fff;
	}

	.scroll-popup {
		height: 300px;
	}

	.no-info {
		text-align: center;
		margin-top: 5px;
	}
</style>